﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <style type="text/css">
        body {
            text-align: center;
        }
    </style>
    <link href="Resource/JavaScript/layui/css/layui.css" rel="stylesheet" />
    <script src="Resource/JavaScript/jquery-1.11.3/jquery.min.js"></script>
    <script src="Resource/JavaScript/layui/layui.all.js"></script>
    <script src="Resource/JavaScript/comm/common.js"></script>
</head>
<body>
    <div style=" margin-left:50px; margin-top:20px;">
        <p style=" text-align:left;">
            <button type="button" class="layui-btn" id="btnUpload">
                <i class="layui-icon">&#xe67c;</i>上传文件
            </button>
            <button type="button" class="layui-btn" id="btnAdd" onclick="add();">
                <i class="layui-icon layui-icon-add-circle-fine"></i>新建文件
            </button>
            <button type="button" class="layui-btn" id="btnWps" onclick="authorize();">
                <i class="layui-icon layui-icon-website"></i>WPS云文档管理
            </button>
            <button type="button" class="layui-btn" id="btnWps" onclick="common.showWpsApiDoc();">
                <i class="layui-icon layui-icon-website"></i>WPS接口文档
            </button>
            <button type="button" class="layui-btn" id="btnWps" onclick="common.showLog(0);">
                <i class="layui-icon   layui-icon-log"></i>操作日志
            </button>
            <button type="button" class="layui-btn" id="btnWps" onclick="common.showLog(1);">
                <i class="layui-icon   layui-icon-log"></i>错误日志
            </button>
            <span style="color:red;">（此列表主要针对文件存储在自己服务器的在线编辑功能，wps云文档请操作【WPS云文档管理】）</span>
        </p>
        <table class="layui-table" style=" width:80%; text-align:center;">
            <!--<colgroup>
                <col width="200">
                <col width="200">
                <col>
            </colgroup>-->
            <thead>
                <tr>
                    <th>文件名</th>
                    <th>类型</th>
                    <th>大小</th>
                    <th>创建时间</th>
                    <th>查看</th>
                    <th>编辑</th>
                    <th>删除</th>
                </tr>
            </thead>
            <tbody id="fileBody">
            </tbody>
        </table>
    </div>
    <script>
        layui.use('upload', function () {
            var upload = layui.upload;
            var token = common.getUrlParam("token");
            //执行实例
            var uploadInst = upload.render({
                accept: "file",
                elem: '#btnUpload' //绑定元素
                , url: '/weboffice/upload?token=' + token//上传接口
                , done: function (res) {
                    //上传完毕回调
                    if (res.Data && res.Data.code == "1") {
                        var content = addFileInfo(res.Data.data);
                        $("#fileBody").append(content);
                    }

                }
                , error: function () {
                    //请求异常回调
                }
            });
        });

        $(function () {
            showlist();
        })

        function showlist() {
            var token = common.getUrlParam("token");
            $.ajax({
                url: "/weboffice/list",
                type: "get",
                async: false,
                cache: false,
                dataType: "json",
                data: {
                    "token": token
                },
                success: function (d) {
                    console.log(d);
                    if (d && d.Code) {
                        if (d.Code == "1") {
                            buildlist(d.Data.list);
                            return false;
                        } else {
                            layer.msg(d.Message);
                            setTimeout(function () {
                                var url = "http://" + window.location.host + "/login.html";
                                window.location.href = url;
                            }, 1000);

                            return false;
                        }
                    }
                },
                error: function (err) {    //url无效，请求失败；有Exception异常，没有捕获时。
                    console.log(err);
                }
            });
        }

        function buildlist(data) {
            $("#fileBody").html("");
            var content = '';
            if (!data) return;
            for (var i = 0; i < data.length; i++) {
                content += addFileInfo(data[i]);
            }
            $("#fileBody").html(content);
        }

        function addFileInfo(data) {
            var content = '';
            content += '<tr id="file_' + data.id + '">';
            content += '<td>' + data.name + '</td>';
            content += '<td>' + data.ext + '</td>';
            content += '<td>' + data.size + '</td>';
            content += '<td>' + data.create_date + '</td>'
            content += '<td>  <i class="layui-icon layui-icon-list" style="font-size: 30px; color: #1E9FFF;" onclick="show(\'' + data.id + '\',\'' + data.ext + '\')"></i> </td>';
            content += '<td>  <i class="layui-icon layui-icon-edit" style="font-size: 30px; color: #1E9FFF;" onclick="edit(\'' + data.id + '\',\'' + data.ext + '\')"></i> </td>';
            content += '<td>  <i class="layui-icon layui-icon-close" style="font-size: 30px; color: #1E9FFF;"  onclick="deleteFile(\'' + data.id + '\')"></i> </td>'
            content += '</tr>';
            return content;
        }

        function show(fileId, ext) {
            if (ext == "jpg" || ext == "png" || ext == "gif" || ext == "jpeg" || ext == "bmp") {
                showPicture(fileId);
            } else {
               var userId = common.getUrlParam("userId");
                var token = common.getUrlParam("token");
                var url = "http://" + window.location.host + "/detail.html?token=" + token + "&fileId=" + fileId + "&permission=read&userId="+userId;
                window.open(url);
            }
        }

        function showPicture(fileId) {
            var token = common.getUrlParam("token");
            $.ajax({
                url: "/weboffice/picture",
                type: "get",
                async: false,
                cache: false,
                dataType: "json",
                data: {
                    "token": token,
                    "fileId": fileId
                },
                success: function (d) {
                    console.log(d);
                    if (d && d.Code) {
                        if (d.Code == "1") {
                            layer.photos({
                                photos: {
                                    "title": d.Data.name, //相册标题
                                    "id": d.Data.id, //相册id
                                    "start": 0, //初始显示的图片序号，默认0
                                    "data": [   //相册包含的图片，数组格式
                                        {
                                            "alt": d.Data.name,
                                            "pid": d.Data.id, //图片id
                                            "src": d.Data.url, //原图地址
                                            "thumb": "" //缩略图地址
                                        }
                                    ]
                                }
                                , anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                            });
                            return false;
                        } else {
                            layer.msg(d.Message);
                            return false;
                        }
                    }
                },
                error: function (err) {    //url无效，请求失败；有Exception异常，没有捕获时。
                    console.log(err);
                }
            });
        }

        function edit(fileId, ext) {
            if (ext == "jpg" || ext == "png" || ext == "gif" || ext == "jpeg" || ext == "bmp") {
                layer.msg("图片不允许进行编辑.");
            } else {
                var token = common.getUrlParam("token");
                var userId = common.getUrlParam("userId");
                var url = "http://" + window.location.host + "/detail.html?token=" + token + "&fileId=" + fileId + "&permission=write&userId="+userId;
                window.open(url);
            }
        }

        function deleteFile(fileId) {
            var token = common.getUrlParam("token");
            $.ajax({
                url: "/weboffice/delete",
                type: "post",
                async: false,
                cache: false,
                dataType: "json",
                data: {
                    "token": token,
                    "fileId": fileId
                },
                success: function (d) {
                    console.log(d);
                    if (d && d.Code) {
                        if (d.Code == "1") {
                            layer.msg(d.Message);
                            $("#file_" + d.Data).remove();
                            return false;
                        } else {
                            layer.msg(d.Message);
                            return false;
                        }
                    }
                },
                error: function (err) {    //url无效，请求失败；有Exception异常，没有捕获时。
                    console.log(err);
                }
            });
        }

        function add() {
            var token = common.getUrlParam("token");
            var userId = common.getUrlParam("userId");
            var url = "http://" + window.location.host + "/new.html?token=" + token + "&userId=" + userId;
            window.open(url);
        }

        function authorize() {
            layer.confirm('请先确认授权的WPS用户已加入测试用户中？', { icon: 3, title: '提示' }, function (index) {
                var token = common.getUrlParam("token");
                $.ajax({
                    url: "/authorize/url",
                    type: "get",
                    async: false,
                    cache: false,
                    dataType: "json",
                    data: {
                        "token": token
                    },
                    success: function (d) {
                        console.log(d);
                        if (d && d.Code) {
                            if (d.Code == "1") {
                                var url = d.Data.authorize_url;
                                window.open(url);
                                return false;
                            } else {
                                layer.msg(d.Message);
                                return false;
                            }
                        }
                    },
                    error: function (err) {    //url无效，请求失败；有Exception异常，没有捕获时。
                        console.log(err);
                    }
                });
                layer.close(index);
            });
        }

        function getYunFile() {
            var token = common.getUrlParam("token");
            var url = "http://" + window.location.host + "/wpslist.html?token=" + token;
            window.open(url);
        }

       
    </script>
</body>
</html>